<?php
namespace Model;
use Kuyuan\WqCore\Model\Model;
class CouponModel extends Model
{
    protected $_fields = array(
        "id",
        "uniacid",
        "uid",
        "name",
        "address",
        "cover",
        "type",
        "type_value",
        "time_type",
        "time",
        "detail",
        "status"
    );

    public function getTypeValueAttr($_value)
    {
        return json_decode($_value,true);
    }

    public function getDetailAttr($_value)
    {
        return htmlspecialchars_decode($_value);
    }

    public function getCoverAttr($_value)
    {
        if($_value){
            return tomedia(MediaModel::where("id",$_value)->value("thumb"));
        }
    }

    /**
     * 卡券value值
     * @param $_value
     * @param $_data
     * @return mixed
     */
    public function getValueAttr($_value,$_data)
    {
        $_value = "";
        $_type_value = $_data["type_value"];
        switch($_data["type"]) {
            case 'dj':
                $_value = $_type_value["value"]."元";
                break;
            case 'mj':
                $_value = "满".$_type_value["face"]."元，减".$_type_value["value"]."元";
                break;
            case 'zk':
                $_value = $_type_value["value"]."折";
                break;
            case 'dh':
            case 'rc':
                $_value = $_type_value["value"];
                break;
        }
        unset($_type_value);
        return $_value;
    }

    /**
     * 字符串卡券类型
     * @param $_value
     * @param $_data
     * @return mixed
     */
    public function getTypeStrAttr($_value,$_data)
    {
        $_type = "";
        switch ($_data["type"]) {
            case 'dj':
                $_type = '代金券';
                break;
            case 'mj':
                $_type = '满减券';
                break;
            case 'dh':
                $_type = '兑换券';
                break;
            case 'zk':
                $_type = '折扣券';
                break;
            case 'rc':
                $_type = '入场券';
                break;
        }
        return $_type;
    }

    /**
     * 获取卡券到期时间字符串
     * @param $_value
     * @param $_data
     */
    public function getTimeStrAttr($_value,$_data)
    {
        $_time_str = "";
        switch ($_data["time_type"]) {
            case "1":
                $_time_str = "永久有效";
                break;
            case "2":
                $_time_str = "卡券有效期至".date("Y-m-d",$_data["time"]);
                break;
            case "3":
                $_time_str = "领券后".$_data["time"]."天内有效";
                break;
        }
        return $_time_str;
    }

    /**
     * 卡券状态
     */
    public function getStatusAttr($_value,$_data)
    {
        $_status = $_value;
        if($_data["time_type"] == 2 && $_data["time"]+86400-1 < TIMESTAMP) {
            $_status = 8;
        }
        return (int) $_status;
    }

    //------------setAttr---------

    /**
     * 设置卡券状态
     */
    public function setCoverAttr($_value,$_data)
    {
        if(isset($_data["cover"]) && $_data["cover"]){
            return $_data["cover"][0];
        } else {
            return null;
        }
    }

    /**
     * 设置卡券面值
     * @param $_value
     * @return false|string
     */
    public function setTypeValueAttr($_value)
    {
        return json_encode($_value);
    }

}